package com.zhengdongzulin.zuke.app.main.mine.collection

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.widget.TextView
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.androidktx.core.getVM
import com.lxj.androidktx.core.vertical
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.WorkListBean
import com.zhengdongzulin.zuke.app.work.activity.ZKWorkListActivity
import com.zhengdongzulin.zuke.app.work.activity.ZKWorkListDetailActivity
import com.zhengdongzulin.zuke.app.work.vm.WorkListFragmentVm
import com.zhengdongzulin.zuke.base.BaseFragment
import com.zhengdongzulin.zuke.databinding.ZkFragmentCollectionFootprintBinding
import com.zhengdongzulin.zuke.ext.put
import com.zhengdongzulin.zuke.ext.start

/**
 * jeff
 * 2021-03-27 17:50:47
 */
class WorkListFragment(var mType: String) :
    BaseFragment<WorkListFragmentVm, ZkFragmentCollectionFootprintBinding>() {

    companion object {
        fun newInstance(type: String) = WorkListFragment(type)
    }

    override fun getLayoutResId(): Int = R.layout.zk_fragment_collection_footprint

    override fun initVM(): WorkListFragmentVm = getVM(WorkListFragmentVm::class.java)

    override fun initView() {
        mAdapter = VListAdpater(mData)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            Intent().put("id", mAdapter.data[position].id)
                .start(requireActivity(), ZKWorkListDetailActivity::class.java)
        }
        mView.recyclerView.vertical().adapter = mAdapter

        mView.contentView.setEnableRefresh(true)
        mView.contentView.setOnRefreshListener {
            mView.contentView.resetNoMoreData()
            mViewModel.mCurrentPage = 1
            getData()
        }
        mView.contentView.setOnLoadMoreListener {
            mViewModel.mCurrentPage++
            getData()
        }
    }

    override fun initData() {
        getData()
    }

    fun getData() {
        mViewModel.getWorklist(mType, (activity as ZKWorkListActivity).incidentType!!)
    }

    override fun initListener() {
    }

    override fun startObserve() {
        mViewModel.getWorklist.observe(this, Observer {
            if (mViewModel.mCurrentPage == 1) {
                mData.clear()
                mView.contentView.finishRefresh()
            }
            it?.let {
                mData.addAll(it)
                if (it.size < 10) {
                    mView.contentView.finishLoadMoreWithNoMoreData()
                } else {
                    mView.contentView.finishLoadMore()
                }
            }
            if (mData.size == 0) {
                mView.statusView.showEmpty()
            } else {
                mView.statusView.showContent()
            }
            mAdapter.notifyDataSetChanged()
        })
        mViewModel.getWorklist.state.observe(this, Observer {
            when (it) {
                StateLiveData.State.Empty -> {
                    mView.statusView.showEmpty()
                }
                StateLiveData.State.Error -> {
                    //toast(mViewModel.looking.errMsg)
                    mView.statusView.showEmpty()
                }
            }
        })
    }

    var mData: MutableList<WorkListBean> = mutableListOf()
    lateinit var mAdapter: VListAdpater

    inner class VListAdpater(
        list: MutableList<WorkListBean>
    ) : BaseQuickAdapter<WorkListBean, BaseViewHolder>(
        R.layout.zk_item_work_list,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: WorkListBean) {

            holder.setText(R.id.tv_content, item.description)
            holder.setText(R.id.tv_status, item.status)
            holder.setText(R.id.tv_type, "${item.incidentType}(${item.title})")

            holder.setText(R.id.tv_user_name, "${item.linkmanName}(${item.linkmanPhone})")
            holder.setText(R.id.tv_user_time, item.lastModifiedDate)

            when (item.incidentTypeImage) {
                "1" -> {
                    holder.setImageResource(R.id.iv_type, R.drawable.zk_icon_work_list_type_baoxiu)
                }
                "2" -> {
                    holder.setImageResource(R.id.iv_type, R.drawable.zk_icon_work_list_type_baoshi)
                }
                "3" -> {
                    holder.setImageResource(R.id.iv_type, R.drawable.zk_icon_work_list_type_fuwu)
                }
            }



            when (item.status) {
                "待分派" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#EF6D65"))
                            cornerRadius = 8.0f
                        })
                }
                "待接单" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#EF6D65"))
                            cornerRadius = 8.0f
                        })
                }
                "待签到" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#FF9900"))
                            cornerRadius = 8.0f
                        })
                }
                "处理中" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#FF9900"))
                            cornerRadius = 5.0f
                        })
                }
                "待审核" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#00AB77"))
                            cornerRadius = 8.0f
                        })
                }
                "待验收" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#1273F7"))
                            cornerRadius = 8.0f
                        })
                }
                "已完成" -> {
                    holder.getView<TextView>(R.id.tv_status)
                        .setBackgroundDrawable(GradientDrawable().apply {
                            setColor(Color.parseColor("#00AB77"))
                            cornerRadius = 8.0f
                        })
                }
            }
        }
    }


}